package cn.edu.wfit.service;

import cn.edu.wfit.entity.Users;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;

@Data
public class LoginUser implements UserDetails, Serializable {

    /*
     * 自定义用户对象
     * */
    private Users user;
    private String[] permission;
    public LoginUser(Users user){
        this.user = user;
    }
    public LoginUser(Users user,String[] Permission){
        this.user = user;
        this.permission = Permission;
    }
    public LoginUser(){}


    /*
     * 权限信息
     * */
    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return AuthorityUtils.createAuthorityList(this.getPermission());
    }

    /*
     * 密码
     * */
    @Override
    @JsonIgnore
    public String getPassword() {
        return user.getPassword();
    }

    /*
     * 用户名
     * */
    @Override
    @JsonIgnore
    public String getUsername() {
        return user.getName();
    }

    /*
     * 表示判断账户是否过期
     * */
    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return true;
    }

    /*
     * 表示判断账户是否被锁定
     * */
    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        return true;
    }

    /*
     * 表示凭证{密码}是否过期
     * */
    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /*
     * 是否可用
     * */
    @Override
    @JsonIgnore
    public boolean isEnabled() {
        return true;
    }
}
